package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.regex.Pattern;

import com.eltima.components.ui.m;

import dao.ComboDao;
import entiey.Combo;
import entiey.Menu;
import util.CastUtil;
import util.JDBCUtil;
import util.ResultSetCreateObject;

public class ComboDaoImpl implements ComboDao,ResultSetCreateObject<Combo> {

	@Override
	public int add(Combo t) {
		int num=JDBCUtil.executeUpdate(
				"insert into combo (foodNo,comboName,spellCode,type,comboIcon,comboPrice,status,description,remark) values(?,?,?,?,?,?,?,?,?)"
				, t.getFoodNo(),t.getComboName(),t.getSpellCode(),t.getType(),t.getComboIcon(),t.getComboPrice(),t.getStatus(),t.getDescription(),t.getRemark());
		return num;
	}

	@Override
	public int delete(int no) {
		int num=JDBCUtil.executeUpdate("delete from combo where id=?", no);
		return num;
	}

	@Override
	public int update(Combo t) {
		int num=JDBCUtil.executeUpdate(
				"update combo set foodNo=?,comboName=?,spellCode=?,type=?,comboIcon=?,comboPrice=?,status=?,description=?,remark=? "
				, t.getFoodNo(),t.getComboName(),t.getSpellCode(),t.getType(),t.getComboIcon(),t.getComboPrice(),t.getStatus(),t.getDescription(),t.getRemark());
		return num;
	}

	@Override
	public List<Combo> findAll() {
		List<Combo> list=JDBCUtil.executeQuery("select * from combo", this);
		return list;
	}

	@Override
	public Combo findById(int id) {
		Combo combo=null;
		List<Combo> list=null;
		try {
			list = JDBCUtil.executeQuery("select * from combo where id=?", this,id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		combo=list.get(0);
		return combo;
	}

	@Override
	public Combo create(ResultSet rs) {
		Combo combo=new Combo();
		try {
			combo.setId(rs.getInt("id"));
			combo.setFoodNo(rs.getInt("foodNo"));
			combo.setComboName(rs.getString("comboName"));
			combo.setSpellCode(rs.getString("spellCode"));
			combo.setType(rs.getInt("type"));
			combo.setComboIcon(rs.getString("comboIcon"));
			combo.setComboPrice(rs.getInt("comboPrice"));
			combo.setStatus(rs.getInt("status"));
			combo.setDescription(rs.getString("description"));
			combo.setRemark(rs.getString("remark"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return combo;
	}

	@Override
	public List<Combo> findLikeAll(String text) {
		String pattern="\\d+";
		int id=0;
		if(Pattern.matches(pattern, text)) {
			id=CastUtil.castInt(text);
		}
		List<Combo> list = JDBCUtil.executeQuery("select * from combo where id=? or comboName like ? or spellCode like ?", this,id,"%"+text+"%","%"+text+"%");
		return list;
	}

	

}
